package com.jn.rxjava.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jn.rx_java_demo.R;
import com.jn.rxjava.adapter.DailyWeatherAdapter;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

import java.util.List;

public class WeatherFuture extends AppCompatActivity {
    private TextView tv_tianqi;
    private TextView tv_wendu;
    private ImageView tv_imageview;
    private TextView  tv_fengsu, tv_humidity;
    private Button btn_back;

    private RecyclerView recyclerView;
    private DailyWeatherAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_future);
        recyclerView = findViewById(R.id.recyclerView002);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setHasFixedSize(true);
        HeConfig.init("HE2410181258281337", "f9ee8160ef0f49b899237acc441a0c94");
        HeConfig.switchToDevService();
        queryWeather("CN101070201");
        btn_back=findViewById(R.id.backn);
        tv_tianqi = findViewById(R.id.textView3);
        tv_wendu = findViewById(R.id.textView4);
        tv_fengsu = findViewById(R.id.textView10);
        tv_humidity = findViewById(R.id.textView13);





        btn_back.setOnClickListener(v -> {
           onBackPressed();
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            if (data != null) {
                String cityId = data.getStringExtra("cityId");
                String cityName = data.getStringExtra("cityName");
                queryWeather(cityId);
            }
        }
    }
    public void queryWeather(String cityId) {
        QWeather.getWeather7D(this, cityId, new QWeather.OnResultWeatherDailyListener() {
            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                Log.e("Weather", "获取7天天气数据时发生错误: " + e.getMessage());
            }

            @Override
            public void onSuccess(WeatherDailyBean result) {
                Log.d("Weather", "7天天气数据获取成功");

                if (result != null && result.getDaily() != null) {
                    final List<WeatherDailyBean.DailyBean> dailyDataList = result.getDaily();
                    Log.d("Weather", "天气数据点数量: " + dailyDataList.size());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter = new DailyWeatherAdapter(dailyDataList, WeatherFuture.this);
                            recyclerView.setAdapter(adapter);
                            Log.d("Weather", "适配器已设置到RecyclerView");
                        }
                    });
                } else {
                    Log.w("Weather", "结果中没有天气数据");
                }
            }
        });

        QWeather.getWeatherNow(this, cityId, Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
            public static final String TAG = "NowWeather";

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ", e);
                System.out.println("Weather Now Error: " + e.getMessage());
            }

            @Override
            public void onSuccess(WeatherNowBean weatherBean) {
                System.out.println("获取天气成功： " + new Gson().toJson(weatherBean));
                if (Code.OK == weatherBean.getCode()) {
                    final WeatherNowBean.NowBaseBean now = weatherBean.getNow();
                    final String tianqi = now.getText();
                    final String wendu = now.getTemp() + "℃";
                    final String fengsu = now.getWindSpeed() + "Km/h";
                    final String humidity = now.getHumidity() + "%";

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_tianqi.setText(tianqi);
                            tv_wendu.setText(wendu);
                            tv_fengsu.setText(fengsu);
                            tv_humidity.setText(humidity);
                        }
                    });
                } else {
                    // 在此查看返回数据失败的原因
                    Code code = weatherBean.getCode();
                    System.out.println("失败代码: " + code);
                }
            }
        });
    }
}
